{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3",
   "language": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "After local assignment: test spam\nAfter nonlocal assignmet: nonlocal spam\nAfter global assignment: nonlocal spam\n"
     ]
    }
   ],
   "source": [
    "def scope_test():\n",
    "\n",
    "    def do_local():\n",
    "        spam = 'local spam'\n",
    "    \n",
    "    def do_nonlocal():\n",
    "        nonlocal spam\n",
    "        spam =  \"nonlocal spam\"\n",
    "    \n",
    "    def do_global():\n",
    "        global spam\n",
    "        spam = \"global spam\"\n",
    "\n",
    "    spam = \"test spam\"   # ls0\n",
    "    do_local() # 执行完后，ls1.spam销毁\n",
    "    print(\"After local assignment:\", spam)\n",
    "    do_nonlocal()\n",
    "    print(\"After nonlocal assignmet:\", spam)\n",
    "    do_global()\n",
    "    print(\"After global assignment:\", spam)\n",
    "\n",
    "\n",
    "scope_test()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "NameError",
     "evalue": "name 'a' is not defined",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-4-203f0fc07fb9>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'a' is not defined"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "b = a\n",
    "a = 2 \n",
    "del a\n",
    "a\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "100\n1\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "\n",
    "def setvalue(arg):\n",
    "    arg = 100\n",
    "    print(arg)\n",
    "\n",
    "setvalue(a)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "100\n100\n"
     ]
    }
   ],
   "source": [
    "a = [1]\n",
    "\n",
    "def setvalue(arg):\n",
    "    arg[0]=100\n",
    "    print(arg[0])\n",
    "\n",
    "setvalue(a)\n",
    "print(a[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "local\nenclosing\nglobal\n"
     ]
    }
   ],
   "source": [
    "b = 'global' # 全局层\n",
    "\n",
    "def test():\n",
    "    b = 'enclosing' # 封装层\n",
    "    def test2():\n",
    "        b = 'local'\n",
    "        print(b) # 最内层\n",
    "    test2()\n",
    "    print(b)\n",
    "\n",
    "test()\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}